import { ref } from 'vue';
import { Type } from 'protobufjs';
import { Messages, DecodeMessage, useLoadProtoGetType, useDecodeBufferHook } from './proto';

const protoSrc = 'proto/list/parse_live.proto';

export const useParseLiveHook = async (typeName: string, url: string = '') => {
  const parseLiveType = ref<Type>();
  const parseLiveMessage = ref<Messages>();
  const parseLiveBuffer = ref<Uint8Array>();
  parseLiveType.value = await useLoadProtoGetType({ typeName, protoSrc });
  parseLiveMessage.value = parseLiveType.value?.create({ url }) as Messages;
  parseLiveBuffer.value = parseLiveType.value?.encode(parseLiveMessage.value).finish();
  return {
    parseLiveType,
    parseLiveMessage,
    parseLiveBuffer
  };
};

// 解析直播内容
export const useDecodeParseLiveResultHook = async ({ uint8Array }: DecodeMessage) => {
  if (!uint8Array) return '';
  const typeName = 'list.ParseLiveResult';
  const decodeData = await useDecodeBufferHook({ protoSrc, typeName, uint8Array });
  return decodeData;
};
